package com.example.neochevbook;

import java.util.ArrayList;
import java.util.Date;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class Ajouterappartement extends Activity {

	private Button Monappartement = null;
	private Button Annuler = null;
	private Button Ajouterappartement = null;
	private Bundle monbundle;
	private String envoie;
	private AsyncTask<String, String, Boolean> mthreadcons = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_ajouterappartement);
		
		
		Monappartement = (Button) findViewById(R.id.buttonMonappartement1);
		Annuler = (Button) findViewById(R.id.buttonConsulterAppartement);
		Ajouterappartement = (Button) findViewById(R.id.buttonAjouterAppartement);
		
		monbundle = this.getIntent().getExtras();
		envoie = monbundle.getString("e");
		
		String[] mesparams2 = {"", "", "http://goshka.fr/ppe4/connexion/importimage_biens.php"};
		mthreadcons = new Connexion(Ajouterappartement.this).execute(mesparams2);
		
		
			
		Toast.makeText(getApplicationContext(), envoie, Toast.LENGTH_LONG).show();

		Monappartement.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent myIntent = new Intent(getApplicationContext(), Modifierappartement.class);
				myIntent.putExtra("e", envoie);
				startActivity(myIntent);
			}
		});
		
		Annuler.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	
		
		/* Ajouterappartement.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent myIntent = new Intent(getApplicationContext(), Modifierappartement.class);
				startActivity(myIntent);
			}
		}); */


	}
	
public void recupimage_biens(StringBuilder sb) {
		
		//Toast.makeText(getApplicationContext(), "1", Toast.LENGTH_LONG).show();

		JsonElement json = new JsonParser().parse(sb.toString());
		//Toast.makeText(getApplicationContext(), "2", Toast.LENGTH_LONG).show();

		JsonArray varray = json.getAsJsonArray();
		Gson gson = new GsonBuilder().setDateFormat("yyyy-mm-dd").create();
		ArrayList<Image_bien> vlist=new ArrayList<Image_bien>(); 
		 for(JsonElement obj : varray )
		    {
			 
			 Image_bien vImage_bien = gson.fromJson(obj.getAsJsonObject(),Image_bien.class);
			 //gson.fromJson(obj.getAsJsonObject(),Utilisateur.class);
			// Toast.makeText(getApplicationContext(), vutilisateur.getNom(), Toast.LENGTH_LONG).show();

			 vlist.add(vImage_bien);
			 
		    }
		Modele vmod=new Modele();
		//vmod.delclient();
		
		
		int j = 0;
		int k = 0;
		int l = 0;


		for(j = 0; j<vlist.size(); j++)		
		{
				
				if(vmod.trouveImage_bien2(vlist.get(j).getId_image()).getImage() == null)
				{

					vmod.addImage_bien2(vlist.get(j));


				}

			}
	}

	
	public void alertmsg(String title, String msg) {
		AlertDialog.Builder adb = new AlertDialog.Builder(this);
		adb.setTitle(title);
		adb.setPositiveButton("Ok", null);
		adb.setMessage(msg);
		adb.show();
		}
	
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.ajouterappartement, menu);
		return true;
	}

}
